package name.jiaosha.air.system.controller;

import java.util.ArrayList;
import java.util.List;

import name.jiaosha.air.AppConstants;
import name.jiaosha.air.company.service.IGoodsService;
import name.jiaosha.air.system.domain.Product;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.ali8ba.common.PageInfo;
import com.ali8ba.common.PinyinToolkit;

/**
 * 产品字典
 * 
 * @author xu
 * 
 */
@Controller
@RequestMapping("/admin")
public class ProductController {

	@Autowired
	private IGoodsService goodsService;

	@RequestMapping("/productList")
	public ModelAndView getProductList() {

		PageInfo<Product> pageInfo = null;

		if (!AppConstants.DEBUG) {
			List<Product> list = new ArrayList<Product>();
			for (int i = 1; i <= 20; i++) {
				Product product = new Product();
				product.setBrand("海尔");
				product.setCategory("挂机");
				product.setFunctionName("制热");
				product.setUnit("台");
				product.setName("海尔小神童" + i);
				product.setTypeCode(" KFRD-26GW" + i);
				product.setPinyin(PinyinToolkit
						.cn2FirstSpell(product.getName()));
				product.setSeer("国家一级");
				product.setPower("1.5P");
				list.add(product);
			}

		} else {

			pageInfo = goodsService.getProductListByComId(AppConstants.comId,
					1, AppConstants.DEFAULT_SIZE);

		}

		ModelMap model = new ModelMap();
		model.addAttribute("root", pageInfo.getItemList());
		model.addAttribute("totalProperty", pageInfo.getTotalProperty());
		return new ModelAndView("jsonView", model);
	}

	@RequestMapping("/createProduct")
	public ModelAndView createPart(@RequestBody Product product) {

		if (!AppConstants.DEBUG) {

		} else {

			product.setName(product.getName().trim());
			product.setTypeCode(product.getTypeCode().trim());
			product.setComId(AppConstants.comId);
			System.out.println(product);
			goodsService.createProduct(product);
		}

		ModelMap model = new ModelMap();
		model.addAttribute("success", true);
		return new ModelAndView("jsonView", model);
	}

	@RequestMapping("/updateProduct")
	public ModelAndView updatePart(@RequestBody Product p) {
		if (!AppConstants.DEBUG) {

		} else {
			Product product = goodsService.getProductById(p.getId());
			if (product != null) {
				product.setName(p.getName().trim());
				product.setTypeCode(p.getTypeCode().trim());
				product.setBrand(p.getBrand());
				product.setCategory(p.getCategory());
				product.setFunctionName(p.getFunctionName());
				product.setPower(p.getPower());
				product.setSeer(p.getSeer());
				product.setUnit(p.getUnit());
				goodsService.updateProduct(product);
			}

		}
		ModelMap model = new ModelMap();
		model.addAttribute("success", true);
		return new ModelAndView("jsonView", model);
	}
}
